import { Tools } from "../../common/Tools";

const { ccclass, property } = cc._decorator;

/** 一定范围内随机移动 */
@ccclass
export default class RectMove extends cc.Component {
    @property({ displayName: "随机移动大小" })
    moveSize: cc.Size = new cc.Size(500, 500);

    @property({ type: cc.Float, displayName: "移动速度" })
    moveSpeed: number = 500;
    @property({ type: cc.Float, displayName: "旋转速度" })
    rotateSpeed: number = 300;

    @property({ type: cc.Float, displayName: "每过几秒旋转一次方向" })
    switchDirTime: number = 3;
    /** 是否移动 */
    isMove: boolean = true;
    moveAngle: number = 0;   //当前移动方向
    toAngle: number = 0;    //要移动到哪个方向
    start() {
        this.moveAngle = this.random(0, 360);
    }
    update(dt: number) {
        this.updateMove(dt);
    }
    /** 刷新移动 */
    updateMove(dt: number) {
        if (!this.isMove) { return; }
        let rad = cc.misc.degreesToRadians(-this.moveAngle);
        let moveX = Math.sin(rad) * this.moveSpeed * dt;
        let moveY = Math.cos(rad) * this.moveSpeed * dt;
        this.node.position = this.node.position.add(cc.v3(moveX, moveY));

        this.node.angle = this.moveAngle;
        this.updateRotate(dt);
    };
    time_SwitchDir: number = 0;
    /** 刷新 蝴蝶的旋转 */
    updateRotate(dt: number) {
        this.time_SwitchDir += dt;

        if (Math.abs(this.node.x) >= this.moveSize.width / 2 || Math.abs(this.node.y) >= this.moveSize.height / 2) {
            this.time_SwitchDir = 0;
            this.toAngle = this.getAngleForPos(cc.v2(this.node.position), cc.v2(0, 0), true);
            // this.toAngle += Tools.random(-30,30);
        }
        if (this.time_SwitchDir >= this.switchDirTime) {   //每过几秒改变方向
            this.time_SwitchDir = Tools.random(0, this.switchDirTime * 0.8, false);
            this.toAngle = Tools.random(0, 360);
        }
        this.moveAngle = this.getAngleRotate(this.moveAngle, this.toAngle, this.rotateSpeed * dt);
    };
    /**
 * 根据旋转速度  获取当前帧的角度 
 * @param curAngle 当前角度
 * @param toAngle 要旋转到的角度
 * @param rotateSpeed 旋转速度
 */
    getAngleRotate(curAngle: number, toAngle: number, rotateSpeed: number): number {
        let tempR = curAngle % 360;
        tempR = curAngle < 0 ? 360 - Math.abs(tempR) : tempR;

        let tempR2 = toAngle < 0 ? 360 - Math.abs(toAngle) : toAngle;
        if (Math.abs(tempR - tempR2) > rotateSpeed && Math.abs(tempR - tempR2) < 360 - rotateSpeed) {
            if (Math.abs(tempR - tempR2) > 180) {
                if (tempR - tempR2 > 180) {
                    return curAngle + rotateSpeed;
                } else {
                    return curAngle - rotateSpeed;
                }
            } else {
                if (tempR >= tempR2) {
                    return curAngle - rotateSpeed;
                } else {
                    return curAngle + rotateSpeed;
                }
            }
        }
        return toAngle;
    };
    /** 获取随机数 */
    public random(x1: number, x2: number, isInteger = true): number {
        if (isInteger) {
            return x1 + Math.floor(Math.random() * (x2 - x1 + 1));
        }
        return Math.random() * (x2 - x1) + x1;
    }
    /** 根据两个点  求角度 */
    public getAngleForPos(pos1: cc.Vec2, pos2: cc.Vec2, isVertical = false): number {
        let rad = 0;
        if (isVertical) {
            rad = -Math.atan2(pos2.x - pos1.x, pos2.y - pos1.y);
        } else {
            rad = Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x);
        }
        return cc.misc.radiansToDegrees(rad);
    }
}

